package com.ol.edu.server.controller.user;


import com.github.pagehelper.PageInfo;
import com.ol.edu.server.entity.ResponseResult;
import com.ol.edu.server.entity.Role;
import com.ol.edu.server.entity.User;
import com.ol.edu.server.entity.UserVo;
import com.ol.edu.server.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Roger
 * @date 2020/9/7 17:11
 * @description
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /*
        分页获取用户数据&条件查询用户数据
     */
    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo) {
        System.out.println(userVo.getUsername() == "");
        PageInfo<User> pageInfo = userService.findAllUserByPage(userVo);
        return new ResponseResult(true, 200, "用户分页&条件查询成功", pageInfo);
    }

    /*
        修改用户状态
     */
    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(User user) {
        Map map = userService.updateUserStatus(user);
        return new ResponseResult(true, 200, "用户状态设置成功", map);
    }

    /*
        用户登录
     */
    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) {
        User user1 = userService.login(user);
        if (user1 == null) {
            return new ResponseResult(true, 400, "登陆失败", null);
        } else {
            HttpSession session = request.getSession();
            String access_token = UUID.randomUUID().toString();
            //把用户ID 和 token值存到session中
            session.setAttribute("access_token", access_token);
            session.setAttribute("user_id", user1.getId());
            session.setAttribute("user", user1);
            session.setMaxInactiveInterval(60 * 30);
            //封装数据返回给前端
            Map<String, Object> map = new HashMap<>(2);
            map.put("access_token", access_token);
            map.put("user_id", user1.getId());
            map.put("user", user1);
            return new ResponseResult(true, 1, "登陆成功", map);
        }
    }

    /*
        获取用户拥有的菜单权限
     */
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(Integer id) {
        List<Role> roleList = userService.findUserRoleById(id);
        return new ResponseResult(true, 200, "回显角色成功", roleList);
    }

    /*
        分配角色
     */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo) {
        userService.userContextRole(userVo);
        return new ResponseResult(true, 200, "分配角色成功", null);
    }
    /*
        获取用户拥有的菜单权限
     */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {
        //从请求头中获取Authorization信息
        String token = request.getHeader("Authorization");
        //和session中的token比较是否一致
        HttpSession session = request.getSession();
        String access_token = (String) session.getAttribute("access_token");
        if (token.equals(access_token)) {
            //如果一致就取出userID
            Integer userId = (Integer) session.getAttribute("user_id");
            ResponseResult result = userService.getUserPermissions(userId);
            return result;
        } else {
            return new ResponseResult(true, 400, "获取用户拥有的权限失败", null);
        }

    }

}
